/*-----------------------------------------------------------------------------
    
    Copyright (C) 2005 T2-Design Corp. All rights reserved.
    
    File: obj_textfield.c
    
    Content: 
    
    History: 
    2005/11/16 by cwh
        Inherited from M3328C code.
-----------------------------------------------------------------------------*/
#include <sys_config.h>

#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libosd/osd_common_draw.h>
#include "osd_lib_internal.h"

//---------------------------- PRIVATE VARIABLES ----------------------------//


//---------------------------- PRIVATE FUNCTIONS ----------------------------//


//---------------------------- PUBLIC FUNCTIONS -----------------------------//

void OSD_SetTextFieldStrPoint(PTEXT_FIELD pCtrl, UINT16*	pStr)
{
	pCtrl->pString = pStr;
}

void OSD_SetTextFieldContent(PTEXT_FIELD pCtrl, UINT32 StringType, UINT32 Value)
{
	UINT16 len;
	char str[16];

	switch(StringType)
	{
		case STRING_ID:
			pCtrl->wStringID = (UINT16)Value;
			return;
		case STRING_NUMBER:
			ComInt2UniStr(pCtrl->pString, Value, 0);
			break;
		case STRING_NUM_PERCENT:
			sprintf(str,"%d%%",Value);
			ComAscStr2Uni(str, pCtrl->pString); 
			break;
		case STRING_ANSI:
			ComAscStr2Uni((UINT8*)Value,pCtrl->pString);
			break;
		case STRING_UTF8:
			ComUTF8Str2Uni((UINT8*)Value,pCtrl->pString);
			break;
		case STRING_UNICODE:
			ComUniStrCopyChar((UINT8 *)pCtrl->pString, (UINT8 *)Value);
			break;
		default:
			return;
	}
}

#ifdef TXT_RUNNER_SUPPORT

void OSD_DrawTextFieldCell_Ext(PTEXT_FIELD pCtrl,UINT8 bStyleIdx,UINT32 nCmdDraw)
{
	lpVSCR 		pVscr;
	struct OSDRect *objframe;	
	//ID_RSC		RscId;
	PWINSTYLE  	lpWinSty;
	UINT32		fgColor;
	UINT8*		pStr;
	OBJECTINFO	RscLibInfo;
	struct OSDRect r;
	
	objframe	= &pCtrl->head.frame;
	lpWinSty 	= g_osd_rsc_info.osd_get_win_style(bStyleIdx);
	fgColor		= lpWinSty->wFgIdx;
	//RscId 		= OSDExt_GetMsgLibId(OSD_GetLangEnvironment());
	if( pCtrl->pString != NULL)
		pStr = (UINT8*)pCtrl->pString;
	else
		pStr = OSD_GetUnicodeString(pCtrl->wStringID);//g_osd_info.osd_get_rsc_data(RscId, pCtrl->wStringID, &RscLibInfo);
	
	pVscr = OSD_DrawObjectFrame(objframe,bStyleIdx);
	r = *objframe;
	if(!(pCtrl->bAlign&C_ALIGN_SCROLL))//Use bX as the offset of string.
	{
		r.uLeft += pCtrl->bX;
		r.uWidth -= pCtrl->bX<<1;
	}
	r.uTop	+= pCtrl->bY;
	r.uHeight -= pCtrl->bY<<1;	
	OSD_DrawText_Ext2(&r,pStr,lpWinSty->wFgIdx,pCtrl->bAlign,pCtrl->head.bFont,pVscr);

		
}

void OSD_GetTextField_width(PTEXT_FIELD pCtrl1,struct OSDRect* pRect1,PTEXT_FIELD pCtrl2,struct OSDRect* pRect2,PTEXT_FIELD pCtrl3,struct OSDRect* pRect3)
{
	struct OSDRect *objframe;	
	struct OSDRect r;
	
	if(pCtrl1==NULL)
		return;
	
	objframe	= &pCtrl1->head.frame;
	r = *objframe;
	MEMCPY(pRect1,&r,sizeof(struct OSDRect));
	
	if(pCtrl2!=NULL)
	{
		objframe	= &pCtrl2->head.frame;
		r = *objframe;
		MEMCPY(pRect2,&r,sizeof(struct OSDRect));
	}
	
	if(pCtrl3!=NULL)
	{	
		objframe	= &pCtrl3->head.frame;
		r = *objframe;
		MEMCPY(pRect3,&r,sizeof(struct OSDRect));	
	}
}

#endif
void OSD_DrawTextFieldCell(PTEXT_FIELD pCtrl,UINT8 bStyleIdx,UINT32 nCmdDraw)
{
	lpVSCR 		pVscr;
	struct OSDRect *objframe;	
	//ID_RSC		RscId;
	PWINSTYLE  	lpWinSty;
	UINT32		fgColor;
	UINT8*		pStr;
	OBJECTINFO	RscLibInfo;
	struct OSDRect r;
    
	objframe	= &pCtrl->head.frame;
	lpWinSty 	= g_osd_rsc_info.osd_get_win_style(bStyleIdx);
	fgColor		= lpWinSty->wFgIdx;
	//RscId 		= OSDExt_GetMsgLibId(OSD_GetLangEnvironment());
	if( pCtrl->pString != NULL)
	{	
		pStr = (UINT8*)pCtrl->pString;
		#ifdef PERSIAN_SUPPORT		//Ben 131008#1	//Ben 140512#1
		xformer_capture_semaphore();
		arabic_unistr_xformer((UINT16*)pStr,FALSE,TRUE);
		xformer_release_semaphore();
		#endif
	}
	else
		pStr = OSD_GetUnicodeString(pCtrl->wStringID);//g_osd_info.osd_get_rsc_data(RscId, pCtrl->wStringID, &RscLibInfo);
	
	pVscr = OSD_DrawObjectFrame(objframe,bStyleIdx);
	r = *objframe;
	if(!(pCtrl->bAlign&C_ALIGN_SCROLL))//Use bX as the offset of string.
	{
		r.uLeft += pCtrl->bX;
		r.uWidth -= pCtrl->bX<<1;
	}
	r.uTop	+= pCtrl->bY;
	r.uHeight -= pCtrl->bY<<1;	

	if(pCtrl->bAlign&C_ALIGN_SCROLL)
	{
		OSD_DrawText_ext(&r,pStr,lpWinSty->wFgIdx,pCtrl->bAlign,pCtrl->head.bFont,pVscr,pCtrl->bX);
	}
    
	else if((lpWinSty->wBgIdx & C_MIX_BG))
	{
		OSD_DrawText_PureColor(&r,pStr,lpWinSty->wFgIdx,lpWinSty->wBgIdx,pCtrl->bAlign,pCtrl->head.bFont,pVscr);
	} 
	
    else
    {
        OSD_DrawText(&r,pStr,lpWinSty->wFgIdx,pCtrl->bAlign,pCtrl->head.bFont,pVscr);
    }
    
}


VACTION OSD_TextFieldKeyMap(POBJECT_HEAD pObj, UINT32 vkey)
{
	VACTION Action = VACT_PASS;

	switch(vkey)
	{
		case V_KEY_ENTER:
			Action = VACT_ENTER;
			break;
		default:
			break;
	}

	return Action;
}

	
PRESULT OSD_TextFieldProc(POBJECT_HEAD pObj, UINT32 msg_type, UINT32 msg,UINT32 param1)
{
	PRESULT Result;
	
	if(msg_type == MSG_TYPE_KEY)
	{
		VACTION Action;
		UINT32 vkey;
		BOOL bContinue;

		Result = OSD_ObjCommonProc(pObj,msg,param1,&vkey,&Action,&bContinue);
		if(!bContinue)
			goto CHECK_LEAVE;

		switch(Action)
		{	
			case VACT_CLOSE:
CLOSE_OBJECT:    		
				Result = OSD_ObjClose(pObj,C_CLOSE_CLRBACK_FLG);
				goto EXIT;
				break;
			default:
				Result = OSD_SIGNAL(pObj, EVN_UNKNOWN_ACTION, (Action<<16) | vkey, param1);
				break;
		}
	}
	else// if(msg_type==MSG_TYPE_EVNT)
	{
		Result = OSD_SIGNAL(pObj,msg_type, msg, param1);
	}

CHECK_LEAVE:
	if(!(EVN_PRE_OPEN == msg_type))
		CHECK_LEAVE_RETURN(Result,pObj);

EXIT:
	return Result;
}

